import Versions from './components/Versions'
import electronLogo from './assets/electron.svg'
import { useEffect } from 'react';
import * as log from 'electron-log/renderer';

function App(): React.JSX.Element {
  const ipcHandle = (): void => window.api.ipcRenderer.send('ping');
  log.info('App.tsx', 'ipcHandle');
  // window.api.logApi.info('test', 'api', 'info');
  // window.api.logApi.warn('test', 'api', 'warn');
  // window.api.logApi.error('test', 'api', 'error');
  // new window.Notification('title', { body: '简单测试' });//不建议，建议使用IPC方式告知主进程进行通知
  useEffect(() => {
    const handlePing1 = (event: any, message: string) => {
      console.log('Received ping1 in renderer:', !!event, message);
    };
    const handlePing2 = (event: any, message: string) => {
      console.log('Received ping1 in renderer pong:', !!event, message);
    };

    const removeListener = window.api.ipcRenderer.on('ping1', handlePing1);
    window.api.ipcRenderer.once('pong', handlePing2);

    const updateDownload = (event: any, version: string) => {
      console.log('Received autoUpdate in renderer:', !!event, version);
      // 还需要通知主进程执行autoUpdater.downloadUpdate();才会进行下载。
      // window.api.autoUpdate();
    };
    const removeUpdate = window.api.ipcRenderer.on('autoUpdate', updateDownload);

    const invokeTest = async () => {
      const result = await window.api.getTest('invokeTest');
      console.log('Received invokeTest in renderer:', result);
    };
    invokeTest();

    return () => {
      removeListener();
      removeUpdate();
    };
  }, []);

  return (
    <>
      <img alt="logo" className="logo" src={electronLogo} />
      <div className="creator">Powered by electron-vite</div>
      <div className="text">
        Build an Electron app with <span className="react">React</span>
        &nbsp;and <span className="ts">TypeScript</span>
      </div>
      <p className="tip">
        Please try pressing <code>F12</code> to open the devTool
      </p>
      <div className="actions">
        <div className="action">
          <a href="https://electron-vite.org/" target="_blank" rel="noreferrer">
            Documentation
          </a>
        </div>
        <div className="action">
          <a target="_blank" rel="noreferrer" onClick={ipcHandle}>
            Send IPC
          </a>
        </div>
      </div>
      <Versions></Versions>
    </>
  )
}

export default App
